<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <div class="area-header">
          <span class="area-header-title">{{ $t('标的公司') }}</span>
        </div>
        <el-col :span="12">
          <el-form-item :label="$t('公司名称')" prop="company.companyName">
            <el-autocomplete
              v-model="form.company.companyName"
              class="inline-input"
              :fetch-suggestions="companyQuerySearch"
              :placeholder="$t('公司名称')"
              :trigger-on-focus="false"
              :maxlength="100"
              @select="companyHandleSelect"
            >
              <template slot="append">
                <a @click="companyHandleSelect">{{ $t('查询工商信息') }}</a>
              </template>
            </el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司简称')" prop="company.companyShortName">
            <el-input v-model="form.company.companyShortName" :placeholder="$t('公司简称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <!--地域-->
        <Area ref="cityZj" :buss-data="form.company" />
        <el-col :span="12">
          <el-form-item :label="$t('注册地-详细地址')" prop="detailsRegistered">
            <el-input v-model="form.company.detailsRegistered" :placeholder="$t('注册地-详细地址')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24" style="margin-top:15px;">
          <div class="area-header">
            <span class="area-header-title">{{ $t('投资信息') }}</span>
          </div>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资金额') + $t('金额单位')" prop="investmentAmount">
            <Money v-model="form.investmentAmount" unit="w" :placeholder="$t('投资金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('返投金额') + $t('金额单位')" prop="reverseInvestmentAmount">
            <Money v-model="form.reverseInvestmentAmount" unit="w" :placeholder="$t('返投金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="bf:otherReverseInvestment" />
    </div>
  </div>
</template>

<script>
    import { saveOtherReverseInvestment, getOtherReverseInvestment } from '@/api/bf/investGroup/otherReverseInvestment'
    import { companyListByName, getCompanyByName } from '@/api/company/base/companyBase'
    import Area from '@/components/Area'
    export default {
        components: {
          Area
        },
        data() {
            return {
                // 表单参数
                form: {
                  company: {}
                },
                // 天眼查查询条件
                tycParam: {
                  id: '',
                  companyName: '',
                  tycId: ''
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  'company.companyName': [{ required: true, message: '必填信息',	trigger: 'blur' }],
                  'company.companyShortName': [{ required: true, message: '必填信息',	trigger: 'blur' }],
                  investmentAmount: [{ required: true, message: '必填信息',	trigger: 'blur' }],
                  reverseInvestmentAmount: [{ required: true, message: '必填信息',	trigger: 'blur' }]
                }
            }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getOtherReverseInvestment(data.id).then(response => {
                        this.form = response.data
                        this.$nextTick(() => {
                          this.$refs.cityZj.initCity()
                          this.$refs.cityZj.initDistrict()
                        })
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    companyId: undefined,
                    company: {
                      companyName: undefined,
                      companyShortName: undefined,
                      provinceRegistered: undefined,
                      cityRegistered: undefined,
                      districtRegistered: undefined,
                      detailsRegistered: undefined
                    },
                    investmentAmount: undefined,
                    reverseInvestmentAmount: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveOtherReverseInvestment(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          // 标的公司查询
          companyQuerySearch(queryString, cb) {
            companyListByName(queryString).then(res => {
              if (res.code === 200) {
                cb(res.data)
              }
            })
          },
          // 标的公司选择后事件
          companyHandleSelect(item) {
            const companyName = this.form.company.companyName
            if (companyName === '' || companyName === undefined) {
              this.msgError(this.$t(this.$t('请先输入公司全称！')))
              return
            }
            this.form.tycId = item.id
            this.tycParam.id = this.form.id
            this.tycParam.tycId = item.id
            this.tycParam.companyName = companyName
            this.loading = true
            getCompanyByName(this.tycParam).then(res => {
              if (res.msg === 'exist') {
                this.form.company.companyName = ''
                this.msgError(this.$t('公司已存在,请重新录入！'))
              } else if (res.msg === 'nofind') {
                this.form.company.companyName = ''
                this.msgError(this.$t('天眼查没有获取到该公司信息！'))
              } else {
                this.form.company = res.data
                this.$nextTick(() => {
                  if (this.$refs.cityZj?.initCity !== undefined) {
                    this.$refs.cityZj.initCity()
                  }
                  if (this.$refs.cityZj?.initDistrict !== undefined) {
                    this.$refs.cityZj.initDistrict()
                  }
                })
                // 关联公司ID
                this.form.companyId = res.data.id
                this.msgSuccess(this.$t('已获取工商数据！'))
              }
              this.loading = false
            })
          }
        }
    }
</script>
